import requests

"""
17.1.2 使用API调用请求数据

`https://api.github.com/search/repositories?q=language:python&sort=stars`

17.1.3 安装 Requests

`python -m pip install --user requests`
"""

# 17.1.4 处理API响应
url = 'https://api.github.com/search/repositories?q=language:python&sort=stars'

"""
`https://docs.github.com/en/rest/overview/media-types?apiVersion=2022-11-28`

1. The most basic media types the API supports are:
```
application/vnd.github+json
application/json
```

2. Note: In the past, we recommended including v3 in your Accept header.
This is no longer required and will have no impact on your API requests.
"""
headers = {'Accept': 'application/vnd.github.star+json'}
r = requests.get(url, headers=headers)
print(f"Status code: {r.status_code}")
response_dict = r.json()
# print(response_dict.keys())

# 17.1.5 处理响应字典
print(f"Total repositories: {response_dict['total_count']}")

repo_dicts = response_dict['items']
print(f"Repositories returned: {len(repo_dicts)}")

repo_dict = repo_dicts[0]
# print(f"\nKeys: {len(repo_dict)}")
# for key in sorted(repo_dict.keys()):
#     print(key)

# 17.1.5 监视API的速率限制
# `https://api.github.com/rate_limit`
